/**
 * Created by Administrator on 2017/8/11.
 */
$(window).ready(function(){
    //获取屏幕高度，全屏显示
    var winHei = $(window).height();
    var winWidth = $(window).width();
    $(".register_bg").height(winHei);
    $(".register_first_bg").height(winWidth*2127/640);
    $(".register_end_bg").height(winHei);

    function Trim(str,is_global)
    {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g,"");
        if(is_global.toLowerCase()=="g")
        {
            result = result.replace(/\s/g,"");
        }
        return result;
    }
    $('.validImg').click(function() {
        $(".validImg").prop("src", "/sys/ImageServlet?time=" + new Date());
    });
    var href=Trim(decodeURI(window.location.href),'g');
    var source='';
    if(href.indexOf('source')!=-1){
        source=href.split('source=')[1].split('?')[0];
    }
    function getMsg(){
        var phone=$('#phone').val();
        var verification=$('#verification').val();
        var validNum=$("#code").val();
        if(phone==""){
            layer.tips("请先输入手机号",'#phone');
            return;
        }
        if (!/^[1][3,4,5,7,8][0-9]{9}$/.exec(phone)) {
            layer.tips("请输入正确的手机号",'#phone');
            return;
        }
        if(validNum==""){
            layer.tips("请先输入验证码",'#code');
            return;
        }
        var that=$('.code_btn');
        time(that);

        //短信接口
        $.ajax({
            type: "POST",
            url: "/registerSendSms",
            data: {
                mobile:phone,
                pictureCode: validNum
            },
            dataType: "json",
            success: function (data) {
                if(data.result=='success'){
                    layer.msg("短信已发送！");
                }else{
                    wait=0;
                    layer.msg(data.messageText);
                }
            }
        });
    };
    $(".code_btn").bind("click", function () {
        getMsg();
    });
    var wait=60;
    function time(o) {
        if (wait == 0) {
            $(".code_btn").bind("click", function () {
                getMsg();
            });
            o.html('获取验证码');
            wait = 60;
            $('.validImg').trigger('click')
        } else {
            $(".code_btn").unbind();
            o.html("重新发送(" + wait + "s)");
            wait--;
            setTimeout(function() {
                    time(o)
                },
                1000)
        }
    }
    $('.register_btn').bind('click',function(){
        var phone=$('#phone').val();
        var verification=$('#mobileCode').val();
        var validNum=$("#code").val();
        var peopleChoice = $('#people-choice').val();
        var password = $('#password').val();
        var invite = $('#invite').val();
        if(phone==""){
            layer.tips("请先输入11位手机号",'#phone');
            return;
        }
        if (!/^[1][3,4,5,7,8][0-9]{9}$/.exec(phone)) {
            layer.tips("请输入正确的手机号",'#phone');
            return;
        }
        if(validNum==""){
            layer.tips("请输入图片验证码",'#code');
            return;
        }
        if(verification==""){
            layer.tips("请先输入验证码",'#mobileCode');
            return;
        }
        if(password==""){
            layer.tips("请先设置密码",'#password');
            return;
        }
        if(password.length<6){
            layer.tips("密码格式不正确",'#password');
            return;
        }
        //图片验证
        //图片验证码
        $('.validImg').click(function() {
            $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
        });
        layer.load();
        $.ajax({
            type: "POST",
            url: "/register/pcRegister",
            data: {
                mobile:phone,
                mobileCode:verification,
                pictureCode: validNum,
                invite:invite,
                password:password,
                source:source
            },
            dataType: "json",
            success: function (data) {
                if(data.result=='success'){
                    $(".register_bg").hide();
                    $(".register_end_bg").show();
                }else{
                    layer.msg(data.messageText);
                }
                setTimeout(function(){
                    layer.closeAll('loading');
                }, 2000);
            }
        });

    });
    $(".register_first_btn").bind("click", function () {
        var mobile=$('#mobile').val();
        if(mobile==""){
            layer.tips("请先输入11位手机号",'#mobile');
            return;
        }
        if (!/^[1][3,4,5,7,8][0-9]{9}$/.exec(mobile)) {
            layer.tips("请输入正确的手机号",'#mobile');
            return;
        }
        $(".register_first_bg").hide();
        $('body').css("overflow","hidden");
        $(".register_bg").show();
        $("#phone").val(mobile);

    });
    $(".register_end_btn").bind("click",function(){
        window.location.href="/index?0?0";
    });
})
